package com.gdut.huayan.controller;

import com.gdut.huayan.entity.dto.PageQuery;
import com.gdut.huayan.entity.dto.Result;
import com.gdut.huayan.entity.po.Location;
import com.gdut.huayan.service.FileService;
import com.gdut.huayan.service.LocationService;
import com.gdut.huayan.validation.group.GroupAdd;
import com.gdut.huayan.validation.group.GroupUpdate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.groups.Default;

@RestController
@RequestMapping("/location")
public class LocationController {

    @Autowired
    private LocationService locationService;

    @Autowired
    private FileService fileService;

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @PostMapping("/getAll")
    public Result getAll(@RequestBody PageQuery pageQuery){
        return Result.success(locationService.getPage(pageQuery));
    }

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @GetMapping("/get")
    public Result get(Long festivalId){
        return Result.success(locationService.get(festivalId));
    }

    //过时
    //@GetMapping("/upload")
//    public Result getUploadLink(){
//        return Result.success(locationService.getUploadLink());
//    }

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @PostMapping("/add")
    public Result addLocation(@RequestBody @Validated({Default.class, GroupAdd.class}) Location location,
                              String imageRequestId,
                              String effectRequestId){
        location.setImageFileId(fileService.getFileStatus(imageRequestId,true));
        location.setEffectiveImageFileId(fileService.getFileStatus(effectRequestId,true));
        locationService.save(location);
        return Result.success();
    }

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @PostMapping("/update")
    public Result updateLocation(@RequestBody @Validated({Default.class, GroupUpdate.class}) Location location){
        locationService.updateById(location);
        return Result.success();
    }

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @DeleteMapping("/delete")
    public Result deleteLocation(Long locationId){
        locationService.removeById(locationId);
        return Result.success();
    }
}
